define(function(require){
    $(function(){
    	/*---------- 通用首页 ----------*/

        
        /* 获取报表数据 */
        var chartData = {pv:[], uv:[], click:[], ctr:[], cost:[], bid:[], profit:[], sale_income:[], date:[]};
        var getChartData = function(url, params, callback){
        	$ajax.ajax({
        		url: url,
        		data: params,
        		//url: '/trp/media/report?media_id=1&org_id=1&start='+params.start+'&end='+params.end,
        		success: function(data){
        			var days = moment(params.end).diff(moment(params.start), 'days')+1;
        			seajs.log('获取 '+days+' 天数据！')
        			
        			chartData = {pv:[], uv:[], click:[], ctr:[], cost:[], bid:[], profit:[], sale_income:[], date:[]};
        			var s = params.start;
        			for(var i=0; i<days; i++){
        				chartData.pv.push(0);
        				chartData.uv.push(0);
        				chartData.click.push(0);
        				chartData.ctr.push(0);
        				chartData.cost.push(0);
        				chartData.bid.push(0);
        				chartData.profit.push(0);
        				chartData.sale_income.push(0);
        				chartData.date.push(s);
        				s = moment(s).add('days', 1).format('YYYY-MM-DD');
        			}
        			
        			if(!semi.utils.isEmpty(data) && !semi.utils.isEmpty(data.data)){
        				$.each(data.data, function(i,n){
        					var index = chartData.date.indexOf(n.date);
        					
        					chartData.pv[index] = parseFloat(n.pv);
        					chartData.uv[index] = parseFloat(n.uv);
        					chartData.click[index] = parseFloat(n.click);
        					chartData.ctr[index] = parseFloat(n.ctr)*1000;
        					chartData.cost[index] = parseFloat(n.cost);
        					chartData.bid[index] = parseFloat(n.bid);
        					chartData.profit[index] = parseFloat(n.profit);
        					chartData.sale_income[index] = parseFloat(n.sale_income);
        				});
        			}
        			seajs.log(chartData);
        			if(!semi.utils.isEmpty(callback) && $.isFunction(callback))callback.call(this, data);
        		},
        		error: function(data){
        			seajs.log('获取报表数据出错，请检查参数配置信息！');
        		}
        	});
        }
        
        /* 图表 */
        var op = {
    			title: '',
        		btn: $('.showchart'),
        		dataurl: '/trp/index/index_report/',
        		idname: '',
        		idvalue: '',
        		other: {},
        		start: semi.daterange.start,
        		end: semi.daterange.end,
        		type: ''
        	}, popChart;
	
        if(semi.utils.isEmpty(op.dataurl))return;
        var refreshDialog = function(){
			$('.name').text($('[name=changelist] :checked').text());
        	$('[name=datatype][value=pv]')[0].checked = true;
        	popChart.clear().setOption({
	            tooltip: {
	                trigger: 'axis',
	                axisPointer: {
	                	type : 'line',
	                	lineStyle : {
	                	  color: '#cccccc',
	                	  width: 1,
	                	  type: 'solid'
	                	}
                	},
                	formatter: function (params) {
                		try{
                			if(params[0][0].trim()=='点击转化率'){
                				return params[0][1]+'<br/>'+params[0][0]+': '+params[0][2] + '‰'
                			}
            			}catch(e){}
                		return params[0][1]+'<br/>'+params[0][0]+': '+params[0][2]
                	}
	            },
	            legend: {
	            	orient:'vertical',
	                data:['曝光量']
	            },
	            grid: {
	            	y: 30,
	            	x2:40,
	            	y2: 30
	            },
	            xAxis: {
	                type: 'category',
	                boundaryGap: false,
	                data: chartData.date,
	                axisLine:{
	                	show: true,
	                	lineStyle: {
	                        color: '#cccccc', 
	                        width: 1
	                    }
	                },
	                axisLabel:{
	                	textStyle:{
	                		color: '#ababab'
	                	}
	                },
	                splitLine: {
	                    show: true,
	                    lineStyle: { 
	                        color: '#ebebeb',
	                        width: 1
	                    }
	                },
	                boundaryGap: [0.1,0.1]
	            },
	            yAxis: {
	                type: 'value',
	                axisLine:{
	                	show: true,
	                	lineStyle: {
	                        color: '#cccccc', 
	                        width: 1
	                    }
	                },
	                axisLabel:{
	                	textStyle:{
	                		color: '#ababab'
	                	},
	                	formatter: function (params) {
	                		try{
	                			var _name = popChart.getOption().legend.data[0].trim();
	                			if(_name=='点击转化率'){
	                				return params + '‰'
	                			}
                			}catch(e){}
	                		return params
	                	}
	                },
	                splitArea: {
	                	show: true,
	                	areaStyle: {color: ['#ffffff','#f6f6f6']}
	                },
	                splitLine: {
	                    show: true,
	                    lineStyle: { 
	                        color: '#ebebeb',
	                        width: 1
	                    }
	                },
	                boundaryGap: [0.1,0.1]
	            },
	            /*toolbox: {
	                show : true,
	                feature : {
	                    magicType : {show: true, type: ['line', 'bar']},
	                    saveAsImage : {show: true}
	                },
	                x: 690
	            },*/
	            series: [
					{
					    name:'曝光量',
					    type:'line',
					    data:chartData.pv
					}
	            ]
	        });
        };
        
        
        getChartData(op.dataurl, $.extend({
    		start: op.start,
    		end: op.end,
    		type: op.type
    	}, semi.utils.newObj(op.idname, op.idvalue), op.other), function(data){
			
    		//初始化图标
	        popChart = echarts.init($('#mediaChart')[0]);
	        $('.daterangebox-ipt').val(op.start + ' 至 ' + op.end).daterangepicker({
	        	initStart: op.start,
	        	initEnd: op.end,
	            format:'YYYY-MM-DD',
	            maxDate: moment().subtract('days', 1).format("YYYY-MM-DD")
	        }, function(start, end, label) {
				getChartData(op.dataurl, $.extend({
					start: start.format('YYYY-MM-DD'),
    	    		end: end.format('YYYY-MM-DD'),
    	    		type: $('[name=type]').val()
		    	}, semi.utils.newObj(op.idname, $('[name=changelist]').val()), op.other),refreshDialog);
	        });
	        
	        $('[name=type]').on('change', function(){
	        	var type = $(this).val(), date = $('.daterangebox-ipt').val().split(' 至 ');
	        	getChartData(op.dataurl, $.extend({
					start: date[0],
    	    		end: date[1],
    	    		type: type
		    	}, semi.utils.newObj(op.idname, $('[name=changelist]').val()), op.other),refreshDialog);
	        }).select2();
	        
			$('[name=datatype]').on('change', function(){
				var type = $(this).val(), name = $(this).parent().text();
				if(!semi.utils.isEmpty()) data = chartData[type];
				var _op = {
	    	            legend: {
	    	                data:[name]
	    	            },
	    	            series: [{
						    name:name,
						    data:chartData[type]
	    				}]
	    	        };
				popChart.setOption(_op);
			});
			
			refreshDialog()
    	});
        
        
        $('.infobox li:odd').addClass('even');
    });
});